#!/bin/bash
export process=kibana
export KIBANA_HOME=/data/tools/kibana-6.4.3-darwin-x86_64
export PATH=/data/tools/kibana-6.4.3-darwin-x86_64/bin:$PATH
export log=$KIBANA_HOME/log.log
# 杀进程最大循环次数
export maxKill15=30
echo "++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"
PID=$(ps -ef | grep $process | grep -v grep | grep -v start | awk '{print $2}')
num=0;
if [ -n "$PID" ]; then
    echo "$process is exist pid=$PID"
    if ps -p $PID >/dev/null; then
        echo "$process is runnig"
    else
        echo " $ process is not running"
    fi
    kill -15 $PID
    # shellcheck disable=SC2233
    while ( [ -n "$PID" ] && [  $maxKill15 -ge $num  ] ); do
      echo "循环 杀掉 $process";
      # shellcheck disable=SC2006
      num=` expr $num + 1 `
      sleep 3
      echo "kill 15 重试次数 $num"
      PID=$(ps -ef | grep $process | grep -v grep | grep -v start | awk '{print $2}')
    done
    if [ -n "$PID" ]; then
       kill -9 $PID
    fi
    echo "启动 $process"
    nohup kibana >> $log &
    echo "启动 $process 成功"
else
    echo "$process is not exist"
    echo "启动 $process"
    nohup kibana >> $log &
fi
echo "----------------------------------------------------------------------------"